package com.guochu.utils;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Author: luc
 * @Date: 2019-10-10 10:52
 */
public class DateHelper {

    /**
     * 比较两个日期的大小
     * 1:date1<date2
     * -1:date1>date2
     * 0:date1=date2
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int compareDate(String date1, String date2) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt1 = df.parse(date1);
        Date dt2 = df.parse(date2);
        if (dt1.getTime() > dt2.getTime()) {
            //date1<date2
            return 1;
        } else if (dt1.getTime() < dt2.getTime()) {
            //date1>date2
            return -1;
        } else {
            //date1=date2
            return 0;
        }
    }

    /**
     * 比较两个日期的大小
     * 1:date1<date2
     * -1:date1>date2
     * 0:date1=date2
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int compareDate(Date date1, Date date2) {
        if (date1.before(date2)) {
            //date1<date2
            return 1;
        } else if (date1.after(date2)) {
            //date1>date2
            return -1;
        } else {
            //date1=date2
            return 0;
        }
    }

    /**
     * 获取指定格式的当前时间
     *
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date getNowDate(String pattern) throws ParseException {
        Date currentTime = new Date();

        // 使用format()方法将日期转换为指定格式的文本
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String dateString = formatter.format(currentTime);

        //将字符串转为
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(dateString);
    }

    /**
     * 日期转换为指定格式的字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getDateString(Date date, String pattern) {

        if (pattern == null || pattern.equals("")) {
            pattern = "yyyy-MM-dd HH:mm:ss:SSS";
        }

        DateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    /**
     * 将字符串转换成日期
     *
     * @param date1
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String date1, String pattern) throws ParseException {

        if (pattern == null || pattern.equals("")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

        DateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.parse(date1);
    }

    /**
     * 添加年数
     *
     * @param date
     * @param year
     * @return
     */
    public static Date addYear(Date date, int year) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.YEAR, year);

        return rightNow.getTime();
    }

    /**
     * 添加月数
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonth(Date date, int month) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH, month);

        return rightNow.getTime();
    }

    /**
     * 添加天数
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDay(Date date, int day) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_MONTH, day);

        return rightNow.getTime();
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author wangzhuang
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * date格式转ISO8601格式
     *
     * @param date
     * @return
     */
    public static String dateToIso(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        return sdf.format(date);
    }

    /**
     * 获取某月总天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDaysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 取当天的最后时间
     *
     * @return
     */
    public static Date getEndTime() {
        Calendar calendar = new GregorianCalendar();
        //一天的结束时间 yyyy:MM:dd 23:59:59
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 取当天的开始时间
     *
     * @return
     */
    public static Date getBeginTime() {
        Calendar calendar = new GregorianCalendar();
        //一天的开始时间 yyyy:MM:dd 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

}
